package com.example.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatis.service.impl.CrudServiceImpl;
import com.example.dao.ImagesDao;
import com.example.dto.ImagesDTO;
import com.example.entity.ImagesEntity;
import com.example.service.ImagesService;
import com.example.utils.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图片列表
 *
 * @author yyyyjinying 1020066395@qq.com
 * @since 1.0-SNAPSHOT 2025-04-29
 */
@Service
public class ImagesServiceImpl extends CrudServiceImpl<ImagesDao, ImagesEntity, ImagesDTO> implements ImagesService {

    @Override
    public QueryWrapper<ImagesEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ImagesEntity> wrapper = new QueryWrapper<>();


        return wrapper;
    }



    @Override
    public Map<String, String> saveUpdateImages(Long id, List<ImagesDTO> imageLists, List<ImagesDTO> thumbnailIimageLists){
        imageLists.forEach(item -> item.setBelongId(id));
        thumbnailIimageLists.forEach(item -> item.setBelongId(id));
        baseDao.delete(new LambdaQueryWrapper<ImagesEntity>().eq(ImagesEntity::getBelongId, id));

        ImagesDTO imagesDTO = imageLists.stream().filter(item -> item.getIsRecommend() != null && item.getIsRecommend().equals(1)).findFirst().orElse(new ImagesDTO());
        ImagesDTO imagesThumbnailDTO = thumbnailIimageLists.stream().filter(item -> item.getIsRecommend() != null && item.getIsRecommend().equals(1)).findFirst().orElse(new ImagesDTO());
        Map<String, String> map = new HashMap<>();
        map.put("imageUrl", imagesDTO.getUrl());
        map.put("thumbnailUrl", imagesThumbnailDTO.getUrl());

        if (CollectionUtil.isNotEmpty(imageLists)) {
            List<ImagesEntity> imagesEntities = ConvertUtils.sourceToTarget(imageLists, ImagesEntity.class);
            super.insertBatch(imagesEntities);
        } else {
            map.put("imageUrl", "");
        }
        if (CollectionUtil.isNotEmpty(thumbnailIimageLists)) {
            super.insertBatch(ConvertUtils.sourceToTarget(thumbnailIimageLists, ImagesEntity.class));
        } else {
            map.put("thumbnailUrl", "");
        }
        return map;
    }
    @Override
    public void save(ImagesDTO dto) {
        ImagesEntity entity = ConvertUtils.sourceToTarget(dto, ImagesEntity.class);
        entity.setImageId(snowflake.nextId()); // 主键id可能有错误
        baseDao.insert(entity);
    }

    @Override
    @Transactional
    public void changeIsRecommend(List<ImagesDTO> images, List<ImagesDTO> thumbnails) {
        for (ImagesDTO image : images) {
            baseDao.changeIsRecommend(image.getIsRecommend(), image.getImageId());
        }
        for (ImagesDTO thumbnail : thumbnails) {
            baseDao.changeIsRecommend(thumbnail.getIsRecommend(), thumbnail.getImageId());
        }
    }

    @Override
    public void deleteAllByBelongId(List<Long> ids) {
        baseDao.deleteAllByImageIds(ids, null);
    }


}